package com.zerdoor.service.impl;

import com.zerdoor.dao.PlatSetDao;
import com.zerdoor.service.PlatSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by xiaoji on 2014/11/20.
 */
@Service
public class PlatSetServiceImpl implements PlatSetService {
    @Autowired
    private PlatSetDao platSetDao;

    @Override
    public List<Map<String, Object>> getTaskTypeInfo(Map<String, Object> map) {
        List<Map<String, Object>> list = platSetDao.getTaskTypeInfo(map);
        List<Map<String, Object>> skills = platSetDao.getTaskTypeSkill();
        //重置技能
        for(Map<String, Object> m :list){
            StringBuffer skillName = new StringBuffer();
            if(m.get("skill") != null && !"".equals(m.get("skill").toString())){
                String[] arr = m.get("skill").toString().split(",");
                for(int i=0;i<arr.length;i++){
                    for(Map<String, Object> s : skills){
                        if(Integer.parseInt(arr[i]) == Integer.parseInt(s.get("minorId").toString())){
                            skillName.append(s.get("skill").toString());
                            if(i != (arr.length-1)){
                                skillName.append(",");
                            }
                            break;
                        }
                    }
                }
            }
            m.put("skillName",skillName.toString());
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> getTaskTypeSkill() {
        return platSetDao.getTaskTypeSkill();
    }

    @Override
    public void addTaskType(Map<String, Object> map) throws Exception {
        platSetDao.addTaskType(map);
    }

    @Override
    public void updateTaskType(Map<String, Object> map) throws Exception {
        platSetDao.updateTaskType(map);
    }

    @Override
    public void deleteTaskType(int tasktypeId) throws Exception {
        Map<String,Object> map = platSetDao.getTaskTypeByPatentId(tasktypeId);
        if(Integer.parseInt(map.get("total").toString()) >0){
            throw new Exception("此类下面有子类！删除失败。");
        }else{
            platSetDao.deleteTaskType(tasktypeId);
        }

    }

}
